package com.markhsiu.MiniMQ.remote.transport.netty.handler;

import com.markhsiu.MiniMQ.broker.message.BrokerFactory;
import com.markhsiu.MiniMQ.model.Message;
import com.markhsiu.MiniMQ.model.ResponseMessage;
import com.markhsiu.MiniMQ.model.constant.MessageSourceEnum;
import com.markhsiu.MiniMQ.remote.ChannelHandler;
import com.markhsiu.MiniMQ.remote.MessageHandler;

public class CustomerNettyMessageHandler implements MessageHandler{

	private static final String BODY = "customer ask";
	 
	@Override
	public void handler(final Message request,ChannelHandler handler) {
		Message message = BrokerFactory.instance().pollMessage(request.getTopic());
		 
		Message response = new ResponseMessage();
		response.setSource(MessageSourceEnum.BROKER);
		response.setTarget(MessageSourceEnum.CUSTOMER);
		response.setMessageID(request.getMessageID());
		response.setTimestamp(System.nanoTime());	
		if(message == null){
			response.setBody(ONWAY.getBytes()); 	
		} else {
			response.setTopic(message.getTopic());
			response.setBody(BODY.getBytes());
		}
		handler.writeAndFlush(response);
	}
		
	

}
